# -*- coding: utf-8 -*-
import bs4
import scrapy
from scrapy import Request
from bs4 import Tag

from boss.items import BossItem


class JobPythonSpider(scrapy.Spider):
    name = 'job_python'
    allowed_domains = ['zhipin.com']

    def start_requests(self):
        # start_urls = ['https://www.zhipin.com/c101270100-p100109/?page=1&ka=page-prev', ]
        yield Request(url='https://www.zhipin.com/c101270100-p100109/?page=1&ka=page-prev', callback=self.parse)

    def parse(self, response):
        bs = bs4.BeautifulSoup(response.text)
        job_info = bs.find('div', attrs={'class': 'job-list'})
        '''
        运行出现AssertionError
        '''
        assert isinstance(job_info, Tag)
        for job in job_info.find('div',attrs={'class':'info-primary'},recursive=True):
            assert isinstance(job, Tag)
            item = BossItem()
            info = job.find('p').text
            print(info)
            # info_list = [info.split('|')]
            # item['addr'] = info_list[0]
            # item[]
            for job1 in job.find('h3', attrs={'class': 'name'},recursive=True):
                assert isinstance(job1,Tag)
                item['position'] = job1.find('div',attrs={'class':'job-title'}).text
                item['salary'] = job1.find('span').text


            print(item)
            yield item


